﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace syc.Fc
{
    public partial class JinDu : System.Web.UI.Page
    {
        private string sql =
@"select ID,日期,工段
,case when 工段=1 then '甲班'
when 工段=2 then '乙班'
when 工段=3 then '丙班'
when 工段=4 then '丁班'
else '其他班组' end as 班组
,dbo.产品品名(产品类别ID) as 产品
,case when 分厂ID=1 then '一分厂'
when 分厂ID=2 then
case when 生产线=1 then '二分厂B1'
else '二分厂B2' end
when 分厂ID=5 then
case when 生产线=1 then '五分厂C1'
else '五分厂C2' end

else '其他分厂' end as 分厂

,锁定,入库捆数,入库重量,填报人,计划支数,计划重量
from 生产进度";

        protected string Notice = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.IsPostBack)
            {
                if ((object)Session["OperatorDID"] != null)
                {
                    int did = (int)Session["OperatorDID"];
                    if (did != 0)
                    {
                        this.FenChang.Text = did.ToString();
                        this.FenChang.Enabled = false;

                        if (did == 1)
                        {
                        }
                        else if (did == 2)
                        {
                            this.Panel4.Visible = true;
                        }
                        else if (did == 5)
                        {
                            this.Panel4.Visible = true;
                        }

                    }
                }

                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
            }

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {

            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string state = "";
            //==================给产品下拉列表一个默认值1==========
            if (this.ChanPin.SelectedValue != "")
            {
                state += " and 产品类别ID=" + this.ChanPin.SelectedValue;
            }
            else
            {
                state += " and 产品类别ID=1";

            }
            //====================================================
            if (this.FenChang.Text != "0")
            {
                state += " and 分厂ID=" + this.FenChang.Text.Replace("'", "");
            }
            if (this.GongDuan.Text != "0")
            {
                state += " and 工段=" + this.GongDuan.Text.Replace("'", "");
            }

            if (this.ShengChanXian.Text != "0")
            {
                state += " and 生产线=" + this.ShengChanXian.Text.Replace("'", "");
            }
            DateTime Dt;
            if (DateTime.TryParse(this.Date1.Text, out Dt))
            {
                state += " and 日期>='" + Dt.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out Dt))
            {
                state += " and 日期<='" + Dt.ToShortDateString() + "'";
            }
            if (state.StartsWith(" and"))
            {
                state = " where " + state.Substring(4);
            }
            state += " order by 日期 desc,工段 asc";

            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = sql + state;
            this.ViewState["SelectCommand"] = sql + state;
            this.GridView1.SelectedIndex = -1;
            this.Button_Delete.Enabled = false;
            this.Button_Edit.Enabled = false;
            this.Button_Lock.Enabled = false;

        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteCommand = "delete from [生产进度] where 锁定=0 and [id]= " + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
            this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;

            if (this.SqlDataSource1.Delete() > 0)
            {
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
                this.Notice = "·删除成功";
            }
            else
            {
                this.Notice = "·删除失败";
            }
        }


        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!(bool)GridView1.DataKeys[GridView1.SelectedIndex].Values[1])
            {
                this.Button_Lock.Enabled = true;
                this.Button_Delete.Enabled = true;
            }
            else
            {
                this.Button_Lock.Enabled = false;
                this.Button_Delete.Enabled = false;
            }
            this.Button_Edit.Enabled = true;
            this.Button_Edit.OnClientClick = "parent.swin2_load('/Fc/JinDu_In.aspx?id="
                + (int)GridView1.DataKeys[GridView1.SelectedIndex].Values[0] + "',640,480);";
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.GongDuan.Text = "0";
            this.ChanPin.Text = "0";
            this.Date1.Text = "";
            this.Date2.Text = "";
            this.Button_Query_Click(null, null);
        }

        protected void Button_Lock_Click(object sender, EventArgs e)
        {
            string id = GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
            this.SqlDataSource1.UpdateCommand = "declare @d date \ndeclare @c int\nselect @d=日期,@c=分厂ID from 生产进度 where [id]="
                + id + "\nupdate 生产进度 set 锁定=1 where 日期=@d and 分厂ID=@c";
            this.SqlDataSource1.Update();
        }

    }
}
